/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene;


import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.utlity.HiLogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrSet;
import ohos.agp.components.StackLayout;
import ohos.app.AbilityContext;
import ohos.app.Context;

/**
 * Created by JiangQi on 11/6/18.
 */
public abstract class LifeCycleFrameLayout extends StackLayout {
    private static final boolean DEBUG = false;
    private static final String TAG = "LifeCycleFrameLayout";

    public LifeCycleFrameLayout(Context context) {
        super(context);
    }

    public LifeCycleFrameLayout(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public LifeCycleFrameLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    
    private NavigationScene mNavigationScene;
    
    private SceneComponentFactory mRootSceneComponentFactory;
    
    private Scope.RootScopeFactory mRootScopeFactory = new Scope.RootScopeFactory() {
        @Override
        public Scope getRootScope() {
            return Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
        }
    };
    private final SceneLifecycleManager<NavigationScene> mLifecycleManager = new SceneLifecycleManager<>();



    public void setNavigationScene(NavigationScene rootScene) {
        this.mNavigationScene = rootScene;
    }

    public void setRootSceneComponentFactory(SceneComponentFactory rootSceneComponentFactory) {
        this.mRootSceneComponentFactory = rootSceneComponentFactory;
    }

    public void setRootScopeFactory(Scope.RootScopeFactory rootScopeFactory) {
        this.mRootScopeFactory = rootScopeFactory;
    }

    
    public NavigationScene getNavigationScene() {
        return mNavigationScene;
    }

    public void onActivityCreated(Intent intent) {
        if (this.mNavigationScene == null) {
            throw new NullPointerException("NavigationScene is null");
        }

        Ability ability = null;
        Context context = getContext();
        while (context instanceof AbilityContext) {
            if (context instanceof Ability) {
                ability = (Ability) context;
                break;
            }
            context = ((AbilityContext) context).getContext();
        }

        if (ability == null) {
            throw new IllegalStateException("cant find Ability attached to this View");
        }
        this.mNavigationScene.setRootSceneComponentFactory(this.mRootSceneComponentFactory);
        this.mLifecycleManager.onActivityCreated(ability,
                this,
                this.mNavigationScene,
                this.mRootScopeFactory,
                isSupportRestore(),
                isSupportRestore() ? intent : null);
    }

    public void onStart() {
        this.mLifecycleManager.onStart();
    }

    public void onResume() {
        this.mLifecycleManager.onResume();
    }

    public void onPause() {
        this.mLifecycleManager.onPause();
    }

    public void onSaveInstanceState(Intent outState) {
        if (!isSupportRestore()) {
            return;
        }
        this.mLifecycleManager.onSaveInstanceState(outState);
    }

    public void onStop() {
        this.mLifecycleManager.onStop();
    }

    public void onDestroyView() {
        this.mLifecycleManager.onDestroyView();
        this.mNavigationScene.setRootSceneComponentFactory(null);
    }

    protected abstract boolean isSupportRestore();

    private void log(String log) {
        if (DEBUG) {
            HiLogUtil.debug(TAG, log);
        }
    }
}
